package org.xkx.demo.netty.hello.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.xkx.demo.netty.hello.client.HelloClientHandler;

/**
 * Hello world!
 */
public class HelloServerApp {
	public static void main(String[] args) {
		// 声明线程池
		EventLoopGroup bootGroup = new NioEventLoopGroup();
		EventLoopGroup workGroup = new NioEventLoopGroup();

		ServerBootstrap boot = new ServerBootstrap();
		boot.group(bootGroup, workGroup);
		boot.channel(NioServerSocketChannel.class);
		boot.childHandler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel ch) throws Exception {
				ch.pipeline().addLast(new HelloServerHandler());
			}
		});

		ChannelFuture future = null;
		try {
			future = boot.bind(8080).sync();
			future.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// 关闭线程池
		bootGroup.shutdownGracefully();
		workGroup.shutdownGracefully();
	}
}
